package com.flyco.modules.stm.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.flyco.common.api.vo.Result;
import com.flyco.common.system.query.QueryGenerator;
import com.flyco.common.aspect.annotation.AutoLog;
import com.flyco.common.util.oConvertUtils;
import com.flyco.modules.stm.entity.StmStock;
import com.flyco.modules.stm.entity.StmStockRecord;
import com.flyco.modules.stm.entity.StmStoreInfo;
import com.flyco.modules.stm.service.IStmStockRecordService;
import com.flyco.modules.stm.service.IStmStockService;

import java.util.Date;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flyco.modules.stm.service.IStmStoreInfoService;
import lombok.extern.slf4j.Slf4j;
import com.flyco.common.system.base.controller.BaseController;
import com.flyco.poi.excel.ExcelImportUtil;
import com.flyco.poi.excel.def.NormalExcelConstants;
import com.flyco.poi.excel.entity.ExportParams;
import com.flyco.poi.excel.entity.ImportParams;
import com.flyco.poi.excel.view.EntityExcelView;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Description: 门店库存
 * @Author: flyco
 * @Date: 2020-10-20
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "门店库存")
@RestController
@RequestMapping("/f/stm/stmStock")
public class StmStockController extends BaseController<StmStock, IStmStockService> {
    @Autowired
    private IStmStockService stmStockService;
    @Autowired
    private IStmStoreInfoService stmStoreInfoService;
    @Autowired
    private IStmStockRecordService stmStockRecordService;

    /**
     * 分页列表查询
     *
     * @param stmStock
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "门店库存-分页列表查询")
    @ApiOperation(value = "门店库存-分页列表查询", notes = "门店库存-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(StmStock stmStock,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<StmStock> queryWrapper = QueryGenerator.initQueryWrapper(stmStock, req.getParameterMap());
        queryWrapper.eq("store_id", 0);
        Page<StmStock> page = new Page<StmStock>(pageNo, pageSize);
        IPage<StmStock> pageList = stmStockService.getStockPage(page, queryWrapper);
        return Result.ok(pageList);
    }


    /**
     * 通过库存id查询门店库存流水
     *
     * @param stmStockRecord
     * @return
     */
    @AutoLog(value = "门店库存流水-通过库存ID查询")
    @ApiOperation(value = "门店库存流水-通过库存ID查询", notes = "门店库存流水-通过库存ID查询")
    @GetMapping(value = "/listStmStockRecordByStockId")
    public Result<?> listStmStockRecordByStockId(StmStockRecord stmStockRecord,
                                                 @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                 HttpServletRequest req) {
        if (oConvertUtils.isNotEmpty(stmStockRecord.getStockId())) {
            QueryWrapper<StmStockRecord> queryWrapper = QueryGenerator.initQueryWrapper(stmStockRecord, req.getParameterMap());
            queryWrapper.orderByDesc("create_time");
            Page<StmStockRecord> page = new Page<StmStockRecord>(pageNo, pageSize);
            IPage<StmStockRecord> pageList = stmStockRecordService.page(page, queryWrapper);
            return Result.ok(pageList);
        } else {
            return Result.error("请选择门店库存记录");
        }
    }

    /**
     * 添加
     *
     * @param stmStock
     * @return
     */
    @AutoLog(value = "门店库存-添加")
    @ApiOperation(value = "门店库存-添加", notes = "门店库存-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody StmStock stmStock) {
        stmStockService.save(stmStock);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param stmStock
     * @return
     */
    @AutoLog(value = "门店库存-编辑")
    @ApiOperation(value = "门店库存-编辑", notes = "门店库存-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody StmStock stmStock) {
        stmStockService.updateById(stmStock);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "门店库存-通过id删除")
    @ApiOperation(value = "门店库存-通过id删除", notes = "门店库存-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) Long id) {
        stmStockService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "门店库存-批量删除")
    @ApiOperation(value = "门店库存-批量删除", notes = "门店库存-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.stmStockService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "门店库存-通过id查询")
    @ApiOperation(value = "门店库存-通过id查询", notes = "门店库存-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) Long id) {
        StmStock stmStock = stmStockService.getById(id);
        return Result.ok(stmStock);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param stmStock
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, StmStock stmStock) {
        return super.exportXls(request, stmStock, StmStock.class, "门店库存");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, StmStock.class);
    }

}
